package cc.bluemc.modules.weixin.action;

import cc.bluemc.modules.weixin.entity.WxQrcordEntity;
import cc.bluemc.modules.weixin.entity.WxQrcordScanEntity;
import cc.bluemc.modules.weixin.service.WxQrcordScanService;
import cc.bluemc.modules.weixin.service.WxQrcordService;
import cc.bluemc.platform.comm.ActionResultMap;
import cc.bluemc.platform.comm.BaseAction;
import cc.bluemc.platform.comm.DataTablesPagination;
import cc.bluemc.platform.jdbctemplatetool.exception.NoColumnAnnotationFoundException;
import cc.bluemc.platform.jdbctemplatetool.exception.NoIdAnnotationFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/wx3p/manage")
public class WxQrcordAction extends BaseAction{
	@Autowired
	private WxQrcordService wxQrcordService;
	@Autowired
	private WxQrcordScanService wxQrcordScanService;
	/*
	*表单页面
	*/
	@RequestMapping("{appId:.+}/qrCord/form")
	public String addForm(Integer id,@PathVariable("appId")String appId,Model model) throws NoIdAnnotationFoundException, NoColumnAnnotationFoundException{
		//TODO houfengli 2017/2/24 16:06 判断appId有效及所属用户
		WxQrcordEntity wxQrcord;
		if(null!=id){
			wxQrcord= wxQrcordService.get(id);
		}else {
			wxQrcord=new WxQrcordEntity();
		}
		wxQrcord.setAppid(appId);
		model.addAttribute("bean", wxQrcord);
		return "weixin/qrCode/form";
	}
	/*
	*保存
	*/
	@RequestMapping("qrCord/save")
	public ActionResultMap save(WxQrcordEntity wxQrcord,Model model){
		try {
//			wxQrcord.setCreateUserId(getAdminUser().getId());
			wxQrcordService.saveOrUpdate(wxQrcord);
			resultMap.setSuccess(true);
			resultMap.setMsg("保存成功！");
		} catch (Exception e) {
			resultMap.setSuccess(false);
			resultMap.setMsg("保存失败");
		}
//		return "redirect:/wx3p/manage/"+wxQrcord.getAppid()+"/qrCord/list";
		return resultMap;
	}
	/**
	 * 列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping("{appId:.+}/qrCord/list")
	public String list( @PathVariable("appId")String appId,Model model){
		model.addAttribute("appid",appId);
		return "weixin/qrCode/list";
	}
	/**
	 * 分布查询数据
	 * @param page
	 * @return
	 */
	@ResponseBody
	@RequestMapping("{appId:.+}/qrCord/pageList")
	public DataTablesPagination listPage(DataTablesPagination page,
										 @PathVariable("appId")String appId,
										 HttpServletRequest request){
		WxQrcordEntity wxQrcord = new WxQrcordEntity();
		wxQrcord.setAppid(appId);
		wxQrcordService.getListOfPage(page,wxQrcord );
		return page;
	}
	/**
	 * 删除
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("del")
	public ActionResultMap del(WxQrcordEntity wxQrcord) throws Exception{
		wxQrcordService.delete(wxQrcord);
		resultMap.setMsg("删除成功！");
		return resultMap;
	}

	/**
	 * 列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping("{appId:.+}/qrCord/scanList")
	public String scanList( @PathVariable("appId")String appId,Model model){
		model.addAttribute("appid",appId);
		return "weixin/qrCode/scanList";
	}
	/**
	 * 分布查询数据
	 * @param page
	 * @return
	 */
	@ResponseBody
	@RequestMapping("{appId:.+}/qrCord/scanPageList")
	public DataTablesPagination scanPageList(@PathVariable("appId")String appId,DataTablesPagination page,HttpServletRequest request){
		WxQrcordScanEntity wxQrcordScan = new WxQrcordScanEntity();

		wxQrcordScanService.getListOfPage(page,wxQrcordScan );
		return page;
	}

	@RequestMapping("{appId:.+}/getQrCode")
	@ResponseBody
	public WxQrcordEntity exportQrCode(HttpServletResponse response, @PathVariable("appId")String appId,String param) {
		WxQrcordEntity wxQrcordEntity=new WxQrcordEntity();
		try {
			wxQrcordEntity=wxQrcordService.getQrcode(param,appId);
		} catch (NoIdAnnotationFoundException e) {
			e.printStackTrace();
		} catch (NoColumnAnnotationFoundException e) {
			e.printStackTrace();
		}
		return wxQrcordEntity;
	}
}
